package com.example.zqh.food;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.platform.commons.util.StringUtils;

import java.util.Scanner;

/**
 * 代码生成器
 */
public class CodeGenerator {
   //读取控制台类容
    public static String scanner(String tip){
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入"+tip+":");
        System.out.println(help.toString());
        if (scanner.hasNext()){
            String ipt = scanner.next();
            if(StringUtils.isNotBlank(ipt)){
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的"+tip+"!");

    }

    //代码生成器对象
    public static void main(String[] args) {
         AutoGenerator mpg = new AutoGenerator();
         //全局配置
         GlobalConfig gc = new GlobalConfig();
         gc.setOutputDir(scanner("请输入项目路径")+"/src/main/java");
         gc.setAuthor("zqh");
        //完成之后是否打开资源管理器
        gc.setOpen(false);
        //重新生成以后是否覆盖文件
        gc.setFileOverride(false);
        gc.setServiceName("%sService");
        //设置主键自动增长
        gc.setIdType(IdType.AUTO);
        //设置data的类型，只能使用java.util.data代替
        gc.setDateType(DateType.ONLY_DATE);
        //开启实体属性swagger2注解
        gc.setSwagger2(true);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        mpg.setGlobalConfig(gc);

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://43.138.31.196:3306/restaurant?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("Git@225217");
        //使用mysql数据库
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.example.zqh");
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setMapper("mapper");
        pc.setEntity("pojo");
        pc.setXml("mapper");
        mpg.setPackageInfo(pc);

        //策列配置
        StrategyConfig strategy = new StrategyConfig();
        //设置那些表要自动生成
        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));

        //实体类名称驼峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //列名名称驼峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //使用简化getter和setter
        strategy.setEntityLombokModel(true);
        //设置controller的api风格 使用RestController
        strategy.setRestControllerStyle(true);
        //驼峰运作字符
        strategy.setControllerMappingHyphenStyle(true);
        //忽略标准生成实体类的前缀
        //strategy.setTablePrefix("t");
        mpg.setStrategy((strategy));
        mpg.execute();
    }

}
